import svelte from 'rollup-plugin-svelte'
import resolve from 'rollup-plugin-node-resolve'
import commonjs from 'rollup-plugin-commonjs'
import babel from 'rollup-plugin-babel'
import { uglify } from 'rollup-plugin-uglify'
import { obfuscator } from 'rollup-obfuscator';
import sveltePreprocess from 'svelte-preprocess';
import postcss from 'rollup-plugin-postcss';
import pkg from '../package.json';

const name = pkg.name
  .replace(/^(@\S+\/)?(svelte-)?(\S+)/, '$3')
  .replace(/^\w/, m => m.toUpperCase())
  .replace(/-\w/g, m => m[1].toUpperCase());

const preprocess = sveltePreprocess({
  scss: {
    includePaths: ['src'],
  },
  postcss: {
    plugins: [require('autoprefixer')],
  },
});

export default {
  input: 'src/main.js',
  output: [
    { file: pkg.main + '.min.mjs', 'format': 'es' },
    { file: pkg.main + '.min.js', 'format': 'umd', name, sourcemap: false }
  ],
  plugins: [
    babel({
      babelrc: false,
      exclude: "node_modules/**",
      presets: [
        [
          '@babel/preset-env'
        ],
      ]
    }),
    svelte({
      preprocess: preprocess,
      compilerOptions: {
        customElement: true
      }
    }),
    //（resolve要放在commonjs之前）
    resolve(),
    commonjs({
      include: 'node_modules/**'
    }),
    postcss({
      extract: 'index.css',
      minimize: true
    }),
    obfuscator(),
    uglify()
  ],
};